package com.wx.day05.Annotation_aop.test2.Work.Dao;

import com.wx.day05.Annotation_aop.test2.Work.Dao.StudentDao;
import com.wx.day05.Annotation_aop.test2.Work.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Repository

public class StuentDaoimpl implements StudentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Student> queryAll() throws Exception {
        String sql = "select id, name, gender, age from students";
        //BeanPropertyRowMapper 列名或者别名要等于数据库中的属性名
        List<Student> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
        return query;
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        String sql = "delete from students where id = ?";
        int flag = jdbcTemplate.update(sql, id);
        if(flag>0){
            System.out.println("Success delete data");
        }
        else{
            System.out.println("Fail delete data");
        }
    }

    @Override
    public void insertData() throws Exception {
        String sql = "insert into students (id, name, gender, age) values (?, ?, ?, ?)";
        int flag = jdbcTemplate.update(sql, 9, "wxwxx", "male", 32);
        if(flag>0){
            System.out.println("Success insert data");
        }
        else{
            System.out.println("Fail insert data");
        }
    }

    @Override
    public void updateDateById(Integer id) throws Exception {
        String sql = "update students set gender = ? where id = ?";
        int flag = jdbcTemplate.update(sql, "male", id);
        if (flag>0){
            System.out.println("Success update data");
        }
        else{
            System.out.println("Fail update data");
        }
    }
}
